home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d27 / dsapb45.arc / PARSPATH.C < prev   
Text File  |  1990-04-07  |  3KB  |  111 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dir.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. void parspath(char * path)
  8. {                                /* this function converts a pathname */
  9.                                 /* into a full pathname */
  10.                                 /* it uses the string you pass it */
  11.     char drive[MAXDRIVE];        /* used for splitting path */
  12.     char dir[MAXDIR];
  13.     char fname[MAXFILE];
  14.     char ext[MAXEXT];
  15.     char curpath[MAXPATH];        /* used to re-merge path */
  16.     char pathtemp[80]="\0",dirtemp[80]="\0"; /* used as temporary strings */
  17.     char tempstr[80];
  18.     register int a;
  19.  
  20.     fnsplit(path,drive,dir,fname,ext);    /* split path */
  21.     if ( (ext[0] == '\0') && (fname[0]!='\0') &&
  22.             ( (dir[(strlen(dir)-1)]=='\\') || (strlen(dir) == 0) ) )
  23.     {                            /* no extension & either */
  24.                                 /* no directory or dir ends with \ */
  25.                                 /* filename is PROBABLY a directory name */
  26.         strcat(dir,fname);
  27.         fname[0] = '\0';
  28.     }
  29.     a=getdisk();
  30.     if (drive[0] == '\0')
  31.     {                        /* if no drive specified, use current drive */
  32.         drive[0]=a+'A';
  33.         drive[1]=':';
  34.         drive[2] = '\0';
  35.     }
  36.     drive[0]=toupper(drive[0]);
  37.     if (dir[0] == '\0')
  38.     {            /* if no directory, use current directory on that drive */
  39.         getcurdir((drive[0]-'A'+1),curpath);
  40.         strcpy(dirtemp,curpath);
  41.     }
  42.     else
  43.     {                            /* if directory, parse dir. */
  44.         register char * x,* y;
  45.  
  46.         getcurdir((drive[0]-'A'+1),curpath);
  47.         y = dir;
  48.         x = y;
  49.         if (y[0]!='\\') strcpy(dirtemp,curpath);/* not root; use current */
  50.         while (*y)
  51.         {
  52.             x=strchr(y,'\\');                    /* use part before 1st \ */
  53.             if (x==NULL) x=y+strlen(y);            /* no \.  use remainder */
  54.             strncpy(tempstr,y,(size_t)(x-y));    /* copy to temp string */
  55.             tempstr[(x-y)]='\0';                /* teminate with null */
  56.  
  57.             if (dirtemp[0] == '\0')
  58.             {                            /* no working directory */
  59.                 if (strcmp(tempstr,".")==0)
  60.                 {                        /* current directory */
  61.                     strcpy(dirtemp,curpath);
  62.                 }
  63.                 else if (strcmp(tempstr,"..")==0)
  64.                 {                        /* parent of current directory */
  65.                     register char * z;
  66.                     strcpy(dirtemp,curpath);            /* get current dir */
  67.                     z=strrchr(dirtemp,'\\');        /* remove last \ */
  68.                     if (z != NULL )
  69.                         *z='\0';                    /* done */
  70.                 }
  71.                 else
  72.                 {                        /* use root directory specified */
  73.                     strcpy(dirtemp,tempstr);
  74.                 }
  75.             }
  76.             else
  77.             {                            /* modify working directory */
  78.                 if (strcmp(tempstr,".")==0)
  79.                 {
  80.                     ;    /* do nothing - already in current directory */
  81.                 }
  82.                 else if (strcmp(tempstr,"..")==0)
  83.                 {            /* remove last directory in working dir */
  84.                     register char * z;
  85.                     z=strrchr(dirtemp,'\\');    /* find last directory */
  86.                     if ( z!= NULL)
  87.                         *z='\0';                /* remove it */
  88.                 }
  89.                 else
  90.                 {                        /* add directory to working */
  91.                     strcat(dirtemp,"\\");
  92.                     strcat(dirtemp,tempstr);
  93.                 }
  94.             }
  95.             y=(x+1);                /* move past \ */
  96.             if ((*x)=='\0') y=x;    /* end if at end of string */
  97.         }
  98.     }
  99.     if (fname[0] == '\0')
  100.     {                                /* no filename - use * */
  101.         strcpy(fname,"*");
  102.     }
  103.     if (ext[0] == '\0')
  104.     {                                /* no extension - use .* */
  105.         strcpy(ext,".*");
  106.     }
  107.     strcpy(dir,"\\");                /* place \ at beginning of dir */
  108.     strcat(dir,dirtemp);            /* copy working dir to dir */
  109.     fnmerge(pathtemp,drive,dir,fname,ext);    /* re-merge pathname */
  110.     strcpy(path,pathtemp);            /* move it to string passed in */
  111. }